package com.yami.shop.controller;

import com.yami.shop.bean.model.MeltCloudUser;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.dto.user.RegisterRongReqDTO;
import com.yami.shop.service.impl.RongYunUserServiceImpl;
import io.rong.models.response.UserGroupQueryResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址
 *
 * @author FrozenWatermelon
 * @date 2020-12-07 15:50:02
 */
@RestController
@RequestMapping("/account/im/user")
@Schema(description = "app-钱包IM用户信息")
public class RongYunUserController {
    @Autowired
    private RongYunUserServiceImpl userService;

    @GetMapping("/userGetTokenBatch")
    @Operation(summary =  "更新用户标识 Token")
    public ServerResponseEntity<Void> userGetTokenBatch() throws Exception {
         userService.userGetTokenBatch();
         return ServerResponseEntity.success();
    }


    /**
     * 获取 Token 方法
     * url  "/user/getToken"
     * docs "http://rongcloud.cn/docs/server.html#getToken"
     *
     * @param registerRongReqVO 用户信息 id,name,portrait(必传)
     * @return TokenResult
     **/
    @PostMapping("/user_get_token")
    @Operation(summary =  "注册用户，生成用户在融云的唯一身份标识 Token")
    public ServerResponseEntity<String> userGetToken(@RequestBody RegisterRongReqDTO registerRongReqVO) throws Exception {
        return userService.userRegister(registerRongReqVO);
    }

    /**
     * 刷新用户信息方法
     * url  "/user/refresh"
     * docs "http://www.rongcloud.cn/docs/server.html#user_refresh"
     *
     * @param registerRongReqVO 用户信息 id name portrait(必传)
     * @return ResponseResult
     **/
    @PostMapping("/user_refresh")
    @Operation(summary =  "刷新用户信息方法", description = "刷新用户信息方法")
    public ServerResponseEntity<String> userRefresh(@RequestBody RegisterRongReqDTO registerRongReqVO) throws Exception {
        return userService.userRefresh(registerRongReqVO);
    }

    /**
     * 查询用户信息方法
     * url  "/user/info"
     * docs "http://www.rongcloud.cn/docs/server.html#user_info"
     *
     * @param userId 用户信息 id (必传)
     * @return UserResult
     */
    @GetMapping("/get_user_info")
    @Operation(summary =  "查询用户信息方法", description = "查询用户信息方法")
    public ServerResponseEntity<MeltCloudUser> getUserInfo(@RequestParam String userId, String current) throws Exception {
        return userService.getUserInfo(userId, current);
    }

    /**
     * Token 失效
     * url  "/user/refresh"
     * docs "https://docs.rongcloud.cn/v4/5X/views/im/server/user/expire.html"
     *
     * @param userIds userId(必传) time(必传)
     * @return ResponseResult
     **/
    @GetMapping("/user_expire")
    @Operation(summary =  "Token 失效", description = "Token 失效")
    public ServerResponseEntity<String> userExpire(@RequestParam List<String> userIds) {
        return userService.userExpire(userIds);
    }

    /**
     * 查询用户所在群组
     * url  "/user/group/query"
     * docs "https://docs.rongcloud.cn/v4/views/im/server/group/basic.html#query"
     *
     * @param userId 用户信息 id (必传)
     * @return UserGroupQueryResult
     * @throws Exception
     */
    @GetMapping("/get_groups")
    @Operation(summary =  "查询用户所在群组", description = "查询用户所在群组")
    public ServerResponseEntity<UserGroupQueryResult> getGroups(@RequestParam String userId) {
        return userService.getGroups(userId);
    }

    /**
     * 用户注销
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping("/abandon")
    @Operation(summary =  "用户注销", description = "用户注销")
    public ServerResponseEntity<String> abandon(@RequestParam String userId) throws Exception {
        return userService.abandon(userId);
    }

    /**
     * 注销用户列表
     *
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping("/abandonList")
    @Operation(summary =  "注销用户列表", description = "注销用户列表")
    public ServerResponseEntity<List<String>> abandonList(@RequestParam int page, @RequestParam int pageSize) {
        return userService.abandonList(page, pageSize);
    }

    /**
     * 用户激活
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping("/activate")
    @Operation(summary =  "用户激活", description = "用户激活")
    public ServerResponseEntity<String> activate(@RequestParam String userId) {
        return userService.activate(userId);
    }

    /**
     * 检查用户在线状态 方法（每秒钟限100次）
     * 请不要频繁循环调用此接口，而是选择合适的频率和时机调用，此接口设置了一定的频率限制。
     * <p>
     * docs http://www.rongcloud.cn/docs/server.html#user_check_online
     *
     * @param userId:用户 id(必传)
     * @return CheckOnlineResult
     **/
    @GetMapping("/checkOnline")
    @Operation(summary =  "检查用户在线状态", description = "检查用户在线状态")
    public ServerResponseEntity<String> checkOnline(@RequestParam String userId) {
        return userService.checkOnline(userId);
    }


}
